package com.huatech.common.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.huatech.common.util.PropertiesUtil;

/**
 * @author lh
 * 	用来解密配置中的密文(重点配置，在这里扩展用户名的解密) setUsername(name)方法对应xml中的一个property属性，
 *  password默认加密不需要重写， 还可以加密url 重写setUrl(url)
 */
@SuppressWarnings("all")
public class DecryptDruidSource extends DruidDataSource {

	Logger logger = LoggerFactory.getLogger(DecryptDruidSource.class);

	@Override
	public void setUsername(String username) {
		try {
			String filePath = DecryptDruidSource.class.getResource("/").getPath() + "mybatis/jdbc.properties";
			String publicKeyText = PropertiesUtil.getValueByKey(filePath, "db.publicKey");
			username = ConfigTools.decrypt(publicKeyText, username);
		} catch (Exception e) {
			logger.error("db.properties.loading", e);
		}
		super.setUsername(username);
	}
}
